package com.punai.Incomedata.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.punai.Incomedata.vo.SrStatisticsVo;
import com.punai.Incomedata.service.SrStatisticsService;
import com.punai.common.utils.StringUtil;
import com.punai.externalccb.domain.EcoCbbidnode;
import com.punai.externalccb.domain.EcoZbstructitem;
import com.punai.externalccb.mapper.EcoCbbidnodeMapper;
import com.punai.externalccb.mapper.EcoZbstructitemMapper;
import org.apache.commons.collections4.ListValuedMap;
import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class SrStatisticsServiceImpl implements SrStatisticsService {
    @Autowired
    private EcoZbstructitemMapper ecoZbstructitemMapper;
    @Autowired
    private EcoCbbidnodeMapper ecoCbbidnodeMapper;

    @Override
    public List<SrStatisticsVo> getItemTabNames(EcoZbstructitem zbstructitem) {
        List<SrStatisticsVo> res = new ArrayList<>();
        List<EcoZbstructitem> all = ecoZbstructitemMapper.selectEcoZbstructitemList(zbstructitem);
        Set<String> collect = all.stream().map(v -> v.getStructid() + "_" + v.getStructname()+"_"+(StringUtil.isNullOrEmpty(v.getShowtype())?"11":v.getShowtype())).collect(Collectors.toSet());
        for (String s : collect) {
            SrStatisticsVo vo = new SrStatisticsVo();
            String[] split = s.split("_");
//            vo.setKey(split[0]);
            vo.setName(split[1]);
            vo.setValue(split[0]);
            res.add(vo);
        }
        if (zbstructitem.getStagetype() != null && zbstructitem.getStagetype().equals("07")){
            SrStatisticsVo vo = new SrStatisticsVo();
            vo.setName("测试指标表01");
            vo.setValue("01");
            res.add(vo);
            SrStatisticsVo vo2 = new SrStatisticsVo();
            vo2.setName("测试指标表02");
            vo2.setValue("02");
            res.add(vo2);
        }
        return res;
    }

    @Override
    public List<SrStatisticsVo> listSrZBTable(SrStatisticsVo vo) {
        List<SrStatisticsVo> res = new ArrayList<>();
        String proId = vo.getProId();
        String stagetype = vo.getStagetype();
        String zbId = vo.getZbid();
        List<EcoCbbidnode> cbBidNodes = ecoCbbidnodeMapper.selectEcoCbbidnodeList(new EcoCbbidnode() {{
            setProId(proId);
            setNodetype(2);
            setStagetype(stagetype);
        }});
        for (EcoCbbidnode cbBidNode : cbBidNodes) {
            SrStatisticsVo treeRoot = new SrStatisticsVo();
            treeRoot.setId(cbBidNode.getId());
            treeRoot.setName(cbBidNode.getNodename());
            List<EcoZbstructitem> all = ecoZbstructitemMapper.selectEcoZbstructitemList(new EcoZbstructitem() {{
                setProId(proId);
                setStagetype(stagetype);
                setStructid(zbId);
                setBidnodeid(cbBidNode.getBidnodeid());
            }});
            if(all.size()==0)continue;
            all.sort(Comparator.comparingInt(EcoZbstructitem::getKeyid));
            res.add(treeRoot);
            ListValuedMap<String, EcoZbstructitem> valuedHashMap = new ArrayListValuedHashMap<>();
            for (EcoZbstructitem zbStructItem : all) {
                if (StringUtil.equals(zbStructItem.getZbpid(), zbId)) zbStructItem.setZbpid("0");
                valuedHashMap.put(zbStructItem.getZbpid(), zbStructItem);
            }
            List<EcoZbstructitem> roots = valuedHashMap.get("0");
            if (CollUtil.isNotEmpty(roots)){
                for (EcoZbstructitem root : roots) {
                    SrStatisticsVo tree = new SrStatisticsVo();
                    BeanUtil.copyProperties(root,tree);
                    String zbid = root.getZbid();
                    res.add(tree);
                    setChildren(zbid,valuedHashMap,tree);
                }
            }
        }
        return res;
    }

    private void setChildren(String zbid, ListValuedMap<String, EcoZbstructitem> valuedHashMap, SrStatisticsVo vo) {
        List<EcoZbstructitem> roots = valuedHashMap.get(zbid);
        List<SrStatisticsVo> list = new ArrayList<>();
        if (CollUtil.isNotEmpty(roots)) {
            for (EcoZbstructitem root : roots) {
                SrStatisticsVo tree = new SrStatisticsVo();
                BeanUtil.copyProperties(root,tree);
                String zbid1 = root.getZbid();
                list.add(tree);
                setChildren(zbid1, valuedHashMap,tree);
            }
            vo.setChildren(list);
        }
    }
}
